import 'dart:core';

import 'package:flutter/material.dart';
import 'package:myapp/bean/article.dart';
import 'package:myapp/util/colors_utils.dart';
import 'package:webview_flutter/webview_flutter.dart';

class ArticleDetailPage extends StatefulWidget {
  final Article article;

  const ArticleDetailPage({Key key, this.article}) : super(key: key);

  @override
  _ArticleDetailPageState createState() => _ArticleDetailPageState();
}

class _ArticleDetailPageState extends State<ArticleDetailPage> {
  double _progress = 0;

  /// 加载js，去掉掘金、简书、CSDN等H5页面的Title、底部操作栏，以及部分广告
  _customJs(String url) {
    StringBuffer js = StringBuffer();
    js.write("javascript:(function(){");
    switch (Uri.parse(url).host) {
      case "juejin.im":
      case "juejin.cn":
        {
          js.write(
              "var followButtonList = document.getElementsByClassName('follow-button');");
          js.write(
              "if(followButtonList&&followButtonList.length){followButtonList[0].parentNode.removeChild(followButtonList[0])}");
          js.write(
              "var articleBannerList = document.getElementsByClassName('article-banner');");
          js.write(
              "if(articleBannerList&&articleBannerList.length){articleBannerList[0].parentNode.removeChild(articleBannerList[0])}");
          js.write(
              "var subscribeBtnList = document.getElementsByClassName('subscribe-btn');");
          js.write(
              "if(subscribeBtnList&&subscribeBtnList.length){subscribeBtnList[0].parentNode.removeChild(subscribeBtnList[0])}");
          js.write(
              "var headerList = document.getElementsByClassName('main-header-box');");
          js.write(
              "if(headerList&&headerList.length){headerList[0].parentNode.removeChild(headerList[0])}");
          js.write(
              "var openAppList = document.getElementsByClassName('open-in-app');");
          js.write(
              "if(openAppList&&openAppList.length){openAppList[0].parentNode.removeChild(openAppList[0])}");
          js.write(
              "var actionBox = document.getElementsByClassName('action-box');");
          js.write(
              "if(actionBox&&actionBox.length){actionBox[0].parentNode.removeChild(actionBox[0])}");
          js.write(
              "var actionBarList = document.getElementsByClassName('action-bar');");
          js.write(
              "if(actionBarList&&actionBarList.length){actionBarList[0].parentNode.removeChild(actionBarList[0])}");
          js.write(
              "var columnViewList = document.getElementsByClassName('column-view');");
          js.write(
              "if(columnViewList&&columnViewList.length){columnViewList[0].style.margin = '0px'}");
          break;
        }
      case "www.jianshu.com":
        {
          js.write(
              "var badgeItemList = document.getElementsByClassName('badge-item');");
          js.write(
              "if(badgeItemList&&badgeItemList.length){badgeItemList[0].parentNode.removeChild(badgeItemList[0])}");
          js.write(
              "var appOpenList = document.getElementsByClassName('app-open');");
          js.write(
              "if(appOpenList&&appOpenList.length){appOpenList[0].parentNode.removeChild(appOpenList[0])}");
          js.write(
              "var jianshuHeader = document.getElementById('jianshu-header');");
          js.write(
              "if(jianshuHeader){jianshuHeader.parentNode.removeChild(jianshuHeader)}");
          js.write(
              "var commentMain = document.getElementById('comment-main');");
          js.write(
              "if(commentMain){commentMain.parentNode.removeChild(commentMain)}");
          js.write("var footer = document.getElementById('footer');");
          js.write("if(footer){footer.parentNode.removeChild(footer)}");
          js.write("var revealAd = document.getElementById('reveal-ad');");
          js.write("if(revealAd){revealAd.parentNode.removeChild(revealAd)}");
          js.write(
              "var headerShimList = document.getElementsByClassName('header-shim');");
          js.write(
              "if(headerShimList&&headerShimList.length){headerShimList[0].parentNode.removeChild(headerShimList[0])}");
          js.write(
              "var fubiaoList = document.getElementsByClassName('fubiao-dialog');");
          js.write(
              "if(fubiaoList&&fubiaoList.length){fubiaoList[0].parentNode.removeChild(fubiaoList[0])}");
          js.write(
              "var ads = document.getElementsByClassName('note-comment-above-ad-wrap');");
          js.write(
              "if(ads&&ads.length){ads[0].parentNode.removeChild(ads[0])}");
          js.write(
              "var lazyShimList = document.getElementsByClassName('v-lazy-shim');");
          js.write(
              "if(lazyShimList&&lazyShimList.length&&lazyShimList[0]){lazyShimList[0].parentNode.removeChild(lazyShimList[0])}");
          js.write(
              "if(lazyShimList&&lazyShimList.length&&lazyShimList[1]){lazyShimList[1].parentNode.removeChild(lazyShimList[1])}");
          js.write(
              "var callAppBtnList = document.getElementsByClassName('call-app-btn');");
          js.write(
              "if(callAppBtnList&&callAppBtnList.length){callAppBtnList[0].parentNode.removeChild(callAppBtnList[0])}");
          break;
        }
      case "blog.csdn.net":
        {
          js.write(
              "var detailFollow = document.getElementById('detailFollow');");
          js.write(
              "if(detailFollow){detailFollow.parentNode.removeChild(detailFollow)}");
          js.write(
              "var csdnToolBar = document.getElementById('csdn-toolbar');");
          js.write(
              "if(csdnToolBar){csdnToolBar.parentNode.removeChild(csdnToolBar)}");
          js.write("var csdnMain = document.getElementById('main');");
          js.write("if(csdnMain){csdnMain.style.margin='0px'}");
          js.write("var operate = document.getElementById('operate');");
          js.write("if(operate){operate.parentNode.removeChild(operate)}");
          js.write(
              "var haveHeartCountList = document.getElementsByClassName('have-heart-count');");
          js.write(
              "if(haveHeartCountList&&haveHeartCountList.length){haveHeartCountList[0].parentNode.removeChild(haveHeartCountList[0])}");
          js.write(
              "var asideHeaderFixedList = document.getElementsByClassName('aside-header-fixed');");
          js.write(
              "if(asideHeaderFixedList&&asideHeaderFixedList.length){asideHeaderFixedList[0].parentNode.removeChild(asideHeaderFixedList[0])}");
          js.write(
              "var feedSignSpanList = document.getElementsByClassName('feed-Sign-span');");
          js.write(
              "if(feedSignSpanList&&feedSignSpanList.length){feedSignSpanList[0].parentNode.removeChild(feedSignSpanList[0])}");
          break;
        }
    }
    js.write("})()");
    return js.toString();
  }

  WebViewController _webViewController;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.article.title),
      ),
      body: Column(
        mainAxisSize: MainAxisSize.max,
        children: [
          if (_progress < 100)
            LinearProgressIndicator(
              color: ColorUtils.color_text_1,
              value: _progress,
              minHeight: 1,
            ),
          Expanded(
            child: WebView(
              initialUrl: widget.article.link,
              javascriptMode: JavascriptMode.unrestricted,
              onPageStarted: (s) {},
              navigationDelegate: (s) {
                return NavigationDecision.prevent;
              },
              onWebViewCreated: (control) {
                _webViewController = control;
              },
              onProgress: (progress) {
                setState(() {
                  _progress = progress.toDouble();
                });
              },
              onPageFinished: (s) {
                // _webViewController
                //     ?.loadUrl(_customJs(widget.article.link));
              },
            ),
          )
        ],
      ),
    );
  }
  @override
  void dispose() {
    super.dispose();
    // _webViewController?.clearCache();
  }
}
